import { observable, action, autorun, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import { message, Modal } from 'antd';

const { TableStore } = WeaTableNew;
import { WeaTools } from 'ecCom';
import * as Apis from '../apis/contact';
import * as addCustomerApis from '../apis/addCustomer';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
const { ls, ss } = WeaTools;
export default class ContactImpStore {
    @observable spinning = false;
    @observable impStep = 0;
    @observable fileList = {}
    @observable importInitData = {};
    @observable importdata = {};
    @observable errorRefs = {
      operator: {},
      file: {},
    }
    @observable showLog = false;
    @observable showLogDeatil = false;
    @observable modifyTableStore = new TableStore();
    @observable modifyTableStoreDetail = new TableStore();
    @observable showSearchAd = false;
    @observable logConditionForm = new WeaForm();
    @observable condition = [];
    @observable form = new WeaForm();

    // 初始化
    @action
    doInit = (params) => {
      Apis.getContactLogImpInitDatas(params).then((data) => {
        if (data.status == 'success') {
          const datainfo = data.datas;
          this.importInitData = datainfo;
          if (datainfo.customerId.value) {
            this.importInitData.sellchanceId.browserConditionParam.dataParams = { customer: datainfo.customerId.value };
          }
          this.importdata = {
            operator: datainfo.operator.value,
            customerId: datainfo.customerId.value,
            sellchanceId: datainfo.sellchanceId.value,
          };
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    // 文件上传
    @action
    importFileInfo = (params = {}) => {
      this.fileList = params;
    }

    // 导入数据存入store
    @action
    saveImportData = (params = {}) => {
      this.importdata = {
        ...this.importdata,
        ...params,
      };
    }

    // 同步数据
    @action
    saveImportInitData = (id, name, type) => {
      this.importInitData[type].value = id;
      this.importInitData[type].showName = name;
      this.importInitData[type].browserConditionParam.replaceDatas = [{ id, name }];
    }

    // 导入数据提交
    @action
    submitImport = () => {
      const _this = this;
      const bool = this.importdata.operator;
      const content = getLabel(22161, '确定要提交吗');

      if (bool) {
        if (this.fileList.fileId) {
          Modal.confirm({
            title: getLabel(15172, '系统提示'),
            content: <div dangerouslySetInnerHTML={{ __html: content }} />,
            onOk() {
              _this.startImportFile({ fileid: _this.fileList.fileId });
            },
          });
        } else {
          this.errorRefs.file.showError();
        }
      } else if (!this.importdata.operator) { // 客户经理
        this.errorRefs.operator.showError();
      }
    }

    startImportFile = (params = {}) => { // 处理上传文件
      this.setImpStep(3);
      this.spinning = true;
      const newParams = { ...this.importdata, ...params };
      Apis.doContactLogImp(newParams).then((data) => {
        this.spinning = false;
        if (data.status == 'failed') {
          if (data.isErreData) {
            Modal.confirm({
              title: getLabel(15172, '系统提示'),
              content: data.msg,
              onOk() {
                jQuery('#downFile').attr('src', `/${data.fileRelativePath}`);
              },
              onCancel() {
                // Apis.crmImport({ "operation": "deleteFile" }).then(data => {
                //     return true;
                // }).catch(err => message.error(err))
              },
            });
          } else {
            Modal.confirm({
              title: getLabel(15172, '系统提示'),
              content: data.msgcode ||data.msg,
              onOk() {
              },
              onCancel() {
              },
            });
          }
        } else {
          this.setImpStep(4);
          const modal = Modal.success({
            title: `${getLabel(129463, '导入成功')}`,
            content: `${getLabel(384061, '共{rownum}条数据，全部导入成功！').replace('{rownum}', `${data.datas}`)}`,
            okText: `${getLabel(383773, '知道了')}`,
          });
        }
      }).catch(err => message.error(err));
    }

    @action setImpStep = (step) => {
      this.impStep = step;
    }

    @action
    initDatas=(params = {}) => { // 初始化高级搜索框
      this.logConditionForm = new WeaForm();
      this.condition = [];
      addCustomerApis.getImportLogConditions(params).then((data) => {
        this.condition = data.datas;
        this.logConditionForm && !this.logConditionForm.isFormInit && this.logConditionForm.initFormFields(data.datas);
        this.showSearchAd = false;
      });
    }
    @action
    clearFormFields=() => {
      this.logConditionForm.reset(); // 清除查询条件值
    }
    @action
    setShowSearchAd=(bool) => { // 显示高级搜索框
      this.showSearchAd = bool;
    }

    @action
    doSearch=(params = {}) => { // 高级查询
      const searchParamsAd = this.logConditionForm.getFormParams();
      const newParams = { ...searchParamsAd, ...params, category: 3 };
      this.getLog(newParams);
    }
    @action
    getLog = (params = {}) => { // 导入日志列表
      addCustomerApis.getToDBLog(params).then((data) => {
        if (data.status == 'failed') {
          Modal.error({
            title: getLabel(15172, '系统提示'),
            content: data.msg,
          });
        } else {
          this.modifyTableStore.getDatas(data.sessionkey, 1);
        }
      });
    }
}
